<template>
  <div>
    <el-table :data="list" border>
      <el-table-column
        prop="id"
        label="id"
        width="50">
      </el-table-column>
      <el-table-column
        prop="collectionId"
        label="集合Id"
        width="100">
      </el-table-column>
      <el-table-column
        prop="hiveDatabase"
        label="hive数据库"
        width="150">
      </el-table-column>
      <el-table-column
        prop="hiveTableName"
        label="hive表名"
        width="200">
      </el-table-column>
      <el-table-column
        prop="name"
        label="集合名称"
        width="150">
      </el-table-column>
      <el-table-column
        prop="who"
        label="触发者"
        width="100">
      </el-table-column>
      <el-table-column
        prop="ifAlert"
        label="报警"
        width="150">
        <template slot-scope="scope">
          <el-button round type="danger"  size="small" v-if="scope.row.ifAlert == true">触发报警</el-button>
          <el-button round type="success" size="small"  v-if="scope.row.ifAlert == false">正常</el-button>
        </template>
      </el-table-column>
      <el-table-column
        prop="statue"
        label="任务状态"
        width="200">
      </el-table-column>
      <el-table-column
        prop="triggerTime"
        label="触发时间"
        width="200">
      </el-table-column>
      <el-table-column
        prop="computedTime"
        label="计算完成时间"
        width="200">
      </el-table-column>
      <el-table-column
        prop="triggerTime"
        label="触发距今"
        width="200">
        <template slot-scope="scope">
          <span>{{ getDateDiff(dateTurnTimestamp(scope.row.triggerTime)) }}</span>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <el-pagination
      :current-page="page"
      :total="total"
      :page-size="limit"
      :page-sizes="[5, 10, 20, 30, 40, 50, 100]"
      style="padding: 30px 0; text-align: center;"
      layout="sizes, prev, pager, next, jumper, ->, total, slot"
      @current-change="fetchData"
      @size-change="changeSize"
    />
  </div>
</template>

<script>
import triggerApi from "@/api/monitor/trigger";

export default {
  data() {
    return {

      who: "admin",
      monitorRuleInfoList: [],
      //规则集合
      ruleCollectionList: [],
      //触发记录集合
      list: [],
      query: {
        page: 1, // 默认页码
        limit: 10, // 每页记录数
      },
      total: 0, // 数据库中的总记录数
      page: 1, // 默认页码
      limit: 10, // 每页记录数
      message: "",
      // triggerDate:""
    };
  },
  // mounted() {
  //   this.init()
  // },
  // 生命周期函数：内存准备完毕，页面尚未渲染
  created() {
    console.log('list created......')
    this.fetchData()
  },
  methods: {
    // 当页码发生改变的时候
    changeSize(size) {
      console.log(size)
      this.limit = size
      this.fetchData(1)
    },
// 加载banner列表数据
    fetchData(page = 1) {
      console.log('翻页。。。' + page)
      // 异步获取远程数据（ajax）
      this.page = page
      /**
       *触发记录
       */
      triggerApi.getAllTrigger(this.page, this.limit).then(response => {
        // console.log("-----" + this.query.startDate)
        this.listLoading = true
        this.list = response.data.records
        // console.log("========tableData"+this.tableData)
        this.total = response.data.total
        this.listLoading = false
      })
    },
    //时间日期转换时间戳
    dateTurnTimestamp(time){
      var date = new Date(time);
      var time1 = date.getTime();
      return time1;
    },
    //计算时差
    getDateDiff(dateTimeStamp) {
      var minute = 1000 * 60;
      var hour = minute * 60;
      var day = hour * 24;
      var halfamonth = day * 15;
      var month = day * 30;
      var now = new Date().getTime();
      var diffValue = now - dateTimeStamp;
      if (diffValue < 0) {
        //若日期不符则弹窗口告之,结束日期不能小于开始日期！
      }
      var monthC = diffValue / month;
      var weekC = diffValue / (7 * day);
      var dayC = diffValue / day;
      var hourC = diffValue / hour;
      var minC = diffValue / minute;
      var triggerDate;
      if (monthC >= 1) {
        triggerDate = parseInt(monthC) + "个月前";
      } else if (weekC >= 1) {
        triggerDate = parseInt(weekC) + "周前";
      } else if (dayC >= 1) {
        triggerDate = parseInt(dayC) + "天前";
      } else if (hourC >= 1) {
        triggerDate = parseInt(hourC) + "个小时前";
      } else if (minC >= 1) {
        triggerDate = parseInt(minC) + "分钟前";
      } else
        triggerDate = "刚刚触发";
      return triggerDate;
    }
  }
}
</script>

